package com.example.myblog1_0.config;

import com.example.myblog1_0.service.security.CustomerServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * @Author: xc
 * @Date: 2018/12/22 18:56
 * @Description:
 **/
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    UserDetailsService customerService(){
        return new CustomerServiceImpl();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(customerService()).passwordEncoder(new BCryptPasswordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/","index","article","/show").permitAll()
                .antMatchers("/leaveMessage","/user").hasAnyRole("USER","ADMIN")
                .antMatchers("/editor","/superAdmin").hasAnyRole("ADMIN")
                .and()

                .formLogin().loginPage("/login").defaultSuccessUrl("/");

        http.csrf().disable();

    }

}
